<?php
namespace pgc;
use \hc\tpl;
use \hc\session;
use \hc\js;
class Role extends tpl{
	public function __construct(){
		parent::__construct();
		session::init();
		$this->role_m=new \model\role_m;
		if(isset($_SESSION['pgcinfo'])){
				$this->pgcinfo=$_SESSION['pgcinfo'];
		}else{
				$url=MODEL_URL."/Login/index.html";
			 echo 	js::to_url($url);exit;
		}
	}
	public function index(){
			$list=$this->role_m->s_all(array());
			$this->assign('listarr',$list);
      $this->display('role/list.tpl');
	}
	//添加角色
	public function roleAdd(){
			if(isset($_POST['ajaxs'])){
				$data=ajaxs_from($_POST['arr']);
				if(!check_regex($data['rolecod'],'isnozh')){
						ajax_return(false,'角色代码:只能为字母和数字');
				}elseif(trim($data['rolename'])==''){
						ajax_return(false,'角色名称不能为空');
				}
				//判断角色代码是否唯一
				$where_unique['rolecod']=array('=',$data['rolecod']);
				if($this->role_m->s_one($where_unique)){
						ajax_return(false,'角色代码已存在:'.$data['rolecod']);
				}
				if(($roleid=$this->role_m->a_one($data))!==false){
						addlog($this->pgcinfo,'成功','创建角色id:'.$roleid);
						ajax_return(true,'添加角色成功');
				}else{
						addlog($this->pgcinfo,'失败','创建角色');
						ajax_return(false,'添加角色失败');
				}
			}
			$this->display('role/add.tpl');
	}
	//编辑角色
	public function roleEdit(){
		if(isset($_GET['role_id'])){
			$where['id']=array('=',$_GET['role_id']);
			$roleList=$this->role_m->s_one($where);
			if(!$roleList){
					echo js::alert('不存在编辑角色');
					echo js::go_back();exit;
			}
			$this->assign('list',$roleList);
			$this->display('role/edit.tpl');
		}elseif(isset($_POST['ajaxsedit'])){
				$data=ajaxs_from($_POST['arr']);
				if(trim($data['rolename'])==''){
						ajax_return(false,'角色名称不能为空');
				}
				$where['id']=array('=',$data['id']);
				if($this->role_m->e_one($where,$data)===false){
					addlog($this->pgcinfo,'失败','编辑角色id:'.$data['id']);
					ajax_return(false,'编辑角色失败');
				}else{
					addlog($this->pgcinfo,'成功','编辑角色id:'.$data['id']);
					ajax_return(true,'编辑角色成功');
				}
		}
		echo js::alert('不存在角色编辑对象');
	}
	//删除栏目
	public function roleDel(){
			if(isset($_POST['id'])){
					$where['id']=array('=',$_POST['id']);
					if($this->role_m->d_one($where)===false){
							addlog($this->pgcinfo,'失败','删除角色id:'.$_POST['id']);
							ajax_return(false,'删除失败');
					}else{
							addlog($this->pgcinfo,'成功','删除角色id:'.$_POST['id']);
							ajax_return(true,'删除成功');
					}
			}
	}
}
?>
